<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ include file="/systeminfo/init.jsp" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="weaver.file.FileUpload" %>
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page"/>
<%@ page import="org.json.JSONObject" %>
<%@ page import="org.json.JSONArray" %>
<jsp:useBean id="Util" class="weaver.general.Util" scope="page"/>
<jsp:useBean id="RecordSet" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="RecordSet1" class="weaver.conn.RecordSet" scope="page"/>
<%
    String method = Util.null2String(request.getParameter("method"));
    if (method.equals("anjiansearch")) {
            int pageNo = Util.getIntValue(request.getParameter("start"), 0) / 20 + 1;
            String sqlwhere = " ";
            String _anhao = Util.null2String(request.getParameter("anhao"));
            _anhao=java.net.URLDecoder.decode(_anhao,"utf-8");
            String _chuangshoulvshi = Util.null2String(request.getParameter("chuangshoulvshi"));
            String _leibie = Util.null2String(request.getParameter("leibie"));
            String _shoufeileixing = Util.null2String(request.getParameter("shoufeileixing"));
            String _kehuquancheng = Util.null2String(request.getParameter("kehuquancheng"));
            String _createdate = Util.null2String(request.getParameter("createdate"));
            String _createdate1 = Util.null2String(request.getParameter("createdate1"));
            if (!_anhao.equals("")) {
                sqlwhere += " and a.anhao like '%" + _anhao + "%' ";
            }
            if (!_chuangshoulvshi.equals("")) {
                sqlwhere += " and ','+cast(a.chuangshoulvshi as varchar(512)) like '%," + _chuangshoulvshi + ",%' ";
            }
            if (!_leibie.equals("")) {
                sqlwhere += " and a.leibie= '" + _leibie + "' ";
            }
            if (!_shoufeileixing.equals("")) {
                sqlwhere += " and a.shoufeileixing= '" + _shoufeileixing + "' ";
            }
            if (!_kehuquancheng.equals("")) {
                sqlwhere += " and a.kehuquancheng= '" + _kehuquancheng + "' ";
            }
            if (!_createdate.equals("")) {
                sqlwhere += " and b.createdate>= '" + _createdate + "' ";
            }
            if (!_createdate1.equals("")) {
                sqlwhere += " and b.createdate<= '" + _createdate1 + "' ";
            }
            String totalsql = "select count(a.requestid) as total \n" +
                    "from formtable_main_21 a,workflow_requestbase b\n" +
                    "where a.requestid=b.requestid \n" + sqlwhere +
                    "and (\n" +
                    "exists(select resourceid from hrmrolemembers where roleid='44' and resourceid=" + user.getUID() + ")\n" +
                    "or b.creater=" + user.getUID() + " \n" +
                    "or ','+cast(hezuolvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                    "or ','+cast(chuangshoulvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                    "or chengbanren='" + user.getUID() + "' \n" +
                    "or exists(select id from anjian_shareinfo where relateditemid=a.requestid and ((sharetype=1 and contents=" + user.getUID() + ") or (sharetype=2 and contents in(select departmentid from HrmResource where id='" + user.getUID() + "'))))\n" +
                    ") \n";
            String sql = "select top 20 * from (SELECT ROW_NUMBER() OVER (ORDER BY id desc) AS RowNumber,a.*,b.currentnodeid,b.lastoperatedate \n" +
                    "from formtable_main_21 a,workflow_requestbase b\n" +
                    "where a.requestid=b.requestid \n" + sqlwhere +
                    "and (\n" +
                    "exists(select resourceid from hrmrolemembers where roleid='44' and resourceid=" + user.getUID() + ")\n" +
                    "or b.creater=" + user.getUID() + " \n" +
                    "or ','+cast(hezuolvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                    "or ','+cast(chuangshoulvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                    "or chengbanren='" + user.getUID() + "' \n" +
                    "or exists(select id from anjian_shareinfo where relateditemid=a.requestid and ((sharetype=1 and contents=" + user.getUID() + ") or (sharetype=2 and contents in(select departmentid from HrmResource where id='" + user.getUID() + "'))))\n" +
                    ") \n" +
                    ") _tmp where RowNumber > " + (pageNo - 1) * 20;
            RecordSet.executeSql(totalsql);
            RecordSet.first();
            String total = RecordSet.getString("total");
            RecordSet.executeSql(sql);
            JSONArray ja = new JSONArray();
            while (RecordSet.next()) {
                JSONObject jo = new JSONObject();
                String requestid = RecordSet.getString("requestid");
                jo.put("requestid", requestid);
                String kehuquancheng = RecordSet.getString("kehuquancheng");
                String weituoren = RecordSet.getString("weituoren");
                String anyou = RecordSet.getString("anyou");
                String anhao = RecordSet.getString("anhao");
                String lastoperatedate = RecordSet.getString("lastoperatedate");
                String hezuolvshi = RecordSet.getString("hezuolvshi");
                String chuangshoulvshi = RecordSet.getString("chuangshoulvshi");
                String shoufeileixing=RecordSet.getString("shoufeileixing");
                if (!kehuquancheng.equals("-1")) {
                    RecordSet1.executeSql("select id,name from CRM_CustomerInfo where id=" + kehuquancheng);
                    if (RecordSet1.next()) {
                        kehuquancheng = "<a href=\"javascript:openCrm('" + kehuquancheng + "')\">" + RecordSet1.getString("name") + "</a>";
                        jo.put("kehuquancheng", kehuquancheng);
                    }
                }
                jo.put("weituoren", weituoren);
                anyou = "<a target='_blank' href=\"/Customization/anjian/ViewAnjian.jsp?pajid=" + requestid + "&CustomerID=" + RecordSet.getString("kehuquancheng") + "\">" + anyou + "</a>";
                jo.put("anyou", anyou);
                jo.put("anhao", anhao);
                jo.put("lastoperatedate", lastoperatedate);
                jo.put("hezuolvshi", ResourceComInfo.getMulResourcename(hezuolvshi));
                jo.put("chuangshoulvshi", ResourceComInfo.getMulResourcename(chuangshoulvshi));
                RecordSet1.executeSql("select sum(a.kpje) as kpje,sum(a.skje) as skje\n" +
                        "                from formtable_main_20 a,workflow_requestbase b,formtable_main_21 c\n" +
                        "                where a.requestid=b.requestid and a.ah=c.requestid\n" +
                        "                and b.deleted=0 and c.requestid="+requestid);
                if(RecordSet1.next()){
                    jo.put("kpje", RecordSet1.getString("kpje"));
                    jo.put("skje", RecordSet1.getString("skje"));
                }

                if (!shoufeileixing.equals("-1")) {
                    RecordSet1.executeSql("select selectvalue,selectname from workflow_selectitem where fieldid='6062' and selectvalue=" + shoufeileixing);
                    if (RecordSet1.next())
                        jo.put("shoufeileixing", RecordSet1.getString("selectname"));
                }
                ja.put(jo);
            }
            JSONObject res = new JSONObject();
            res.put("totalcount", total);
            res.put("result", ja);
            //System.out.println(sql);
            //System.out.println(res.toString());
            out.clear();
            out.print(res.toString());
            return;
        }
    if (method.equals("batchshare")) {
        int pageNo = Util.getIntValue(request.getParameter("start"), 0) / 20 + 1;
        String sqlwhere = " ";
        String _anhao = Util.null2String(request.getParameter("anhao"));
        _anhao=java.net.URLDecoder.decode(_anhao,"utf-8");
        String _chuangshoulvshi = Util.null2String(request.getParameter("chuangshoulvshi"));
        String _leibie = Util.null2String(request.getParameter("leibie"));
        String _shoufeileixing = Util.null2String(request.getParameter("shoufeileixing"));
        String _kehuquancheng = Util.null2String(request.getParameter("kehuquancheng"));
        String _createdate = Util.null2String(request.getParameter("createdate"));
        String _createdate1 = Util.null2String(request.getParameter("createdate1"));
        if (!_anhao.equals("")) {
            sqlwhere += " and a.anhao like '%" + _anhao + "%' ";
        }
        if (!_chuangshoulvshi.equals("")) {
            sqlwhere += " and cast(a.chuangshoulvshi as varchar(512)) like '%," + _chuangshoulvshi + ",%' ";
        }
        if (!_leibie.equals("")) {
            sqlwhere += " and a.leibie= '" + _leibie + "' ";
        }
        if (!_shoufeileixing.equals("")) {
            sqlwhere += " and a.shoufeileixing= '" + _shoufeileixing + "' ";
        }
        if (!_kehuquancheng.equals("")) {
            sqlwhere += " and a.kehuquancheng= '" + _kehuquancheng + "' ";
        }
        if (!_createdate.equals("")) {
            sqlwhere += " and b.createdate>= '" + _createdate + "' ";
        }
        if (!_createdate1.equals("")) {
            sqlwhere += " and b.createdate<= '" + _createdate1 + "' ";
        }
        String totalsql = "select count(a.requestid) as total \n" +
                "from formtable_main_21 a,workflow_requestbase b\n" +
                "where a.requestid=b.requestid \n" + sqlwhere +
                "and (\n" +
                "exists(select resourceid from hrmrolemembers where roleid='44' and resourceid=" + user.getUID() + ")\n" +
                "or b.creater=" + user.getUID() + " \n" +
                "or ','+cast(hezuolvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                "or ','+cast(chuangshoulvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                "or chengbanren='" + user.getUID() + "' \n" +
                "or exists(select id from anjian_shareinfo where relateditemid=a.requestid and ((sharetype=1 and contents=" + user.getUID() + ") or (sharetype=2 and contents in(select departmentid from HrmResource where id='" + user.getUID() + "'))))\n" +
                ") \n";
        String sql = "select top 20 * from (SELECT ROW_NUMBER() OVER (ORDER BY id desc) AS RowNumber,a.*,b.currentnodeid,b.lastoperatedate \n" +
                "from formtable_main_21 a,workflow_requestbase b\n" +
                "where a.requestid=b.requestid \n" + sqlwhere +
                "and (\n" +
                "exists(select resourceid from hrmrolemembers where roleid='44' and resourceid=" + user.getUID() + ")\n" +
                "or b.creater=" + user.getUID() + " \n" +
                "or ','+cast(hezuolvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                "or ','+cast(chuangshoulvshi as varchar(512))+',' like '%," + user.getUID() + ",%' \n" +
                "or chengbanren='" + user.getUID() + "' \n" +
                "or exists(select id from anjian_shareinfo where relateditemid=a.requestid and ((sharetype=1 and contents=" + user.getUID() + ") or (sharetype=2 and contents in(select departmentid from HrmResource where id='" + user.getUID() + "'))))\n" +
                ") \n" +
                ") _tmp where RowNumber > " + (pageNo - 1) * 20;
        RecordSet.executeSql(totalsql);
        RecordSet.first();
        String total = RecordSet.getString("total");
        RecordSet.executeSql(sql);
        JSONArray ja = new JSONArray();
        while (RecordSet.next()) {
            JSONObject jo = new JSONObject();
            String requestid = RecordSet.getString("requestid");
            jo.put("requestid", requestid);
            String kehuquancheng = RecordSet.getString("kehuquancheng");
            String weituoren = RecordSet.getString("weituoren");
            String anyou = RecordSet.getString("anyou");
            String anhao = RecordSet.getString("anhao");
            String lastoperatedate = RecordSet.getString("lastoperatedate");
            String hezuolvshi = RecordSet.getString("hezuolvshi");
            String chuangshoulvshi = RecordSet.getString("chuangshoulvshi");
            if (!kehuquancheng.equals("-1")) {
                RecordSet1.executeSql("select id,name from CRM_CustomerInfo where id=" + kehuquancheng);
                if (RecordSet1.next()) {
                    kehuquancheng = "<a href=\"javascript:openCrm('" + kehuquancheng + "')\">" + RecordSet1.getString("name") + "</a>";
                    jo.put("kehuquancheng", kehuquancheng);
                }
            }
            jo.put("weituoren", weituoren);
            anyou = "<a target='_blank' href=\"/Customization/anjian/ViewAnjian.jsp?pajid=" + requestid + "&CustomerID=" + RecordSet.getString("kehuquancheng") + "\">" + anyou + "</a>";
            jo.put("anyou", anyou);
            jo.put("anhao", anhao);
            jo.put("lastoperatedate", lastoperatedate);
            jo.put("hezuolvshi", ResourceComInfo.getMulResourcename(hezuolvshi));
            jo.put("chuangshoulvshi", ResourceComInfo.getMulResourcename(chuangshoulvshi));
            ja.put(jo);
        }
        JSONObject res = new JSONObject();
        res.put("totalcount", total);
        res.put("result", ja);
        //System.out.println(sql);
        //System.out.println(res.toString());
        out.clear();
        out.print(res.toString());
        return;
    }

%>

